<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/1/13
 * Time: 16:19
 */

namespace SDXApi\Controller;


use SDXApi\Service\CategoryService;

/**
 * Class CategoryController
 * @package SDXApi\Controller
 */
class CategoryController extends AppController
{

	/**
	 * Created by Jerry
	 */
	public function get_categories()
	{
        $version = $this->getVersion();
	    $cacheName = 'all_category_list_' . $version;
        $data = S($cacheName);

        if(empty($data)){
            $categoryService = new CategoryService();
            $data = $categoryService->getCategories();
            S($cacheName, $data, 3600);
        }

		if (empty($data)) {
			returnApiError('分类数据为空');
		}

		$arr = array(
			'category_list' => $data,
		);

		returnApiSuccess($arr);
	}

	/**
	 * Created by Jerry
	 */
	public function get_category_by_id()
	{
		$cat_id = I('post.cat_id', 0, 'int');

        $data = S('category_child_info_by_id_'.$cat_id);

        if(empty($data)){
            $categoryService = new CategoryService();
            $data = $categoryService->getCategoryById($cat_id);
            S('category_child_info_by_id_'.$cat_id,$data,3600);
        }

		if (empty($data)) {
			returnApiError('分类数据为空');
		}

		returnApiSuccess($data);
	}

	protected function getVersion()
    {
        $is_app = I('post.is_app');
        $is_ios = I('post.is_ios');
        $version = I('post.version');

        if ($is_ios) {
            return 'ios_' . $version;
        }

        if ($is_app) {
            return 'app_' . $version;
        }

        return 'h5';
    }
}